﻿define(function (require, exports) {
    $(function () {

        //TAB1 列表
        var onSelectPageFun = function (page, pageSize) {
            appMonth = "";
            $("#appList tbody").empty();
            var $loadingRow = getLoadRow(4);
            $.ajax({
                url: "../VitualPhone/GetVitualPhoneList",
                data: { appMonth: appMonth, page: page, pageSize: pageSize, directorId: $("#txtDirectorId").val() },
                dataType: "json",
                beforeSend: function () {
                    $loadingRow.appendTo("#appList tbody");
                },
                type: "post",
                success: function (result) {

                    if (result.length == 0) {
                        var $tr = $("<tr><td colspan=12>暂无数据</td></tr>");
                        $tr.appendTo("#appList tbody");
                        return;
                    }

                    $.each(result, function (index, item) {
                        var $tr = $("<tr></tr>");
                        $tr.append("<td>" + item.Director.Id + "</td>");
                        $tr.append("<td>" + item.Director.Name + "</td>");
                        $tr.append("<td>" + item.AjkNeed + "</td>");
                        $tr.append("<td>" + item.SfNeed + "</td>");
                        //$tr.append("<td>" + item.AppDes + "</td>");
                        //$tr.append("<td>" + item.VitualPhoneDes + "</td>");
                        $tr.appendTo("#appList tbody");
                    });
                },
                complete: function () {
                    $loadingRow.remove();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus + errorThrown);
                }
            });
        };

        var getAppListCount = function () {
            var count = 0;
            $.ajax({
                url: "../VitualPhone/GetVitualPhoneListCount",
                data: { directorId: $("#txtDirectorId").val() },
                async: false,
                type: 'post',
                dataType: "json",
                success: function (result) {
                    count = parseInt(result);
                }
            });
            return count;
        }

        $('#pp').pagination({
            total: getAppListCount(),
            pageSize: 15,
            onSelectPage: onSelectPageFun,
            showPageList: false,
            beforePageText: "页",
            afterPageText: "/ {pages}",
            displayMsg: "显示{from} 到 {to} 共 {total} 项"
        });
        onSelectPageFun(1, 15, '', ''); //加载首页数据

        $("#btnSearch").click(function () {
            onSelectPageFun(1, 15);
            $('#pp').pagination({
                total: getAppListCount(),
                pageSize: 15,
                onSelectPage: onSelectPageFun,
                showPageList: false,
                beforePageText: "页",
                afterPageText: "/ {pages}",
                displayMsg: "显示{from} 到 {to} 共 {total} 项"
            });
        });
        //TAB1 列表 END

        //TAB2 列表

        //上传弹窗
        var buttons = [{
            iconCls: 'icon-add',
            handler: vpAddHanler
        }];
        function vpAddHanler() {
            $("#loadUppanel").show();
            $("#loadUppanel #content").load("../Common/UpLoadPartPage");
        }
        $(".closeSpan").click(function () {
            $(this).parents(".loadUppanel").hide();
        });
        //end 上传弹窗
        var onSelectPageFun2 = function (page, pageSize) {
            $("#vitualPhonelist tbody").empty();
            var $loadingRow = getLoadRow(6);
            $.ajax({
                url: "../VitualPhone/GetVitualPhoneDetailList",
                data: { page: page, pageSize: pageSize, directorId: $("#txtDirectorId2").val(), status: $("#selectStatus option:selected").val() },
                dataType: "json",
                beforeSend: function () {
                    $loadingRow.appendTo("#vitualPhonelist tbody");
                },
                type: "post",
                success: function (result) {

                    if (result.length == 0) {
                        var $tr = $("<tr><td colspan=5>暂无数据</td></tr>");
                        $tr.appendTo("#vitualPhonelist tbody");
                        return;
                    }

                    $.each(result, function (index, item) {
                        var status = item.AssignStatus == 1 ? "已分配" : "待分配";
                        var $tr = $("<tr></tr>");
                        $tr.append("<td>" + item.DirectorId + "</td>");
                        $tr.append("<td>" + item.VitualPhoneNum + "</td>");
                        $tr.append("<td>" + item.PhoneKind.Name + "</td>");
                        $tr.append("<td>" + (item.ActulPhoneNum == null ? "暂无" : item.ActulPhoneNum) + "</td>");
                        $tr.append("<td>" + status + "</td>");
                        $tr.append("<td>" + (item.CounselorId == null ? "暂无" : item.CounselorId) + "</td>");
                        $tr.appendTo("#vitualPhonelist tbody");
                    });
                },
                complete: function () {
                    $loadingRow.remove();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus + errorThrown);
                }
            });
        };

        var getAppListCount2 = function () {
            var count = 0;
            $.ajax({
                url: "../VitualPhone/GetVitualPhoneDetailListCount",
                data: { directorId: $("#txtDirectorId2").val(), status: $("#selectStatus option:selected").val() },
                async: false,
                type: 'post',
                dataType: "json",
                success: function (result) {
                    count = parseInt(result);
                }
            });
            return count;
        }

        $('#pp2').pagination({
            total: getAppListCount2(),
            pageSize: 15,
            onSelectPage: onSelectPageFun2,
            showPageList: false,
            beforePageText: "页",
            afterPageText: "/ {pages}",
            displayMsg: "显示{from} 到 {to} 共 {total} 项",
            buttons: buttons
        });
        onSelectPageFun2(1, 15, '', ''); //加载首页数据

        $("#btnSearch2").click(function () {
            onSelectPageFun2(1, 15);
            $('#pp2').pagination({
                total: getAppListCount2(),
                pageSize: 15,
                onSelectPage: onSelectPageFun2,
                showPageList: false,
                beforePageText: "页",
                afterPageText: "/ {pages}",
                displayMsg: "显示{from} 到 {to} 共 {total} 项"
            });
        });
        //TAB2 列表END

    });

});